local GameOverPanel =import("..others.GameOver")
local scheduler = import("framework.scheduler")
local Fish =import("..others.Fish")
local uiLayer = class("uiLayer",function()  return display.newLayer() end) 
function uiLayer:ctor()
--fish
 self.fish =Fish.new()
 self:addChild(self.fish)
 self:setTouchEnabled(true) 
 self:addTouchEventListener(handler(self,self.onTouchEvent),true)  

--qipao
 local qipao= display.newSprite("#paopao0001.png",0,0);
 local qipaoFrame =display.newFrames("paopao00%02d.png",1,60)
 local qipaoAnimation =display.newAnimation(qipaoFrame, 1.0 / 60) 
 transition.playAnimationForever(qipao,qipaoAnimation)
-- self:addChild(qipao)  
 self.qipao =qipao
 
--xiaoqipao 
 local xiaoqipao= display.newSprite("#xiaoqi0001.png",0,0);
 local xiaoqipaoFrame =display.newFrames("xiaoqi00%02d.png",1,60)
 local xiaoqipaoAnimation =display.newAnimation(xiaoqipaoFrame, 1.0 / 60) 
 transition.playAnimationForever(xiaoqipao,xiaoqipaoAnimation)
 self:addChild(xiaoqipao)  
 
 self.xiaoqipao =xiaoqipao
 
 display.align(self.xiaoqipao,display.BOTTOM_CENTER,math.random(0,display.width),math.random(0,display.height));
 display.align(self.qipao,display.BOTTOM_CENTER,math.random(0,display.width),0);
 --score
 local score =display.newSprite("#score.png",0,0)
 self:addChild(score) 
 display.align(score,display.LEFT_TOP,10,display.height-10)
 
print("ui",ui)
 self.scorelabel = ui.newTTFLabel({
    text = "表现 0",
    font ="Microsoft YaHei",
    size = 64,
    align = ui.TEXT_ALIGN_CENTER 
  })  
 self:addChild(self.scorelabel)
 display.align(self.scorelabel,display.LEFT_TOP,12,display.height-10)
 self.currentScore =0
 self.g=4500
 

 
 -------------------------------------
 self.gameOverPanel =GameOverPanel.new()
 print("GameOverPanel",GameOverPanel)
 self.gameOverPanel:setVisible(false)
 self:addChild(self.gameOverPanel)
 self.gameOverPanel:setPosition(CCPoint(0,20))
 
 
end



function uiLayer:getFishRect()
   return self.fish:getFishRect()
end

function uiLayer:addScore(score)
 self.currentScore=self.currentScore+score
 self.scorelabel:setString("表现 "..self.currentScore)
end
function uiLayer:changeScore()
 self.currentScore=self.currentScore+1
 self.scorelabel:setString("表现 "..self.currentScore)
end

function uiLayer:onUpdate()
 display.align(self.xiaoqipao,display.BOTTOM_CENTER,math.random(0,display.width),math.random(0,display.height));
 display.align(self.qipao,display.BOTTOM_CENTER,math.random(0,display.width),0);
end

function uiLayer:animationUpdate()
--   if viewController:isRush() then --加速模式下，不控制鱼的移动，加速后，重新开始
--      viewController:resetTime()
--      self.lastTime =nil
--      print("animationUpdate return becauseOf Rush")
--      return
--   end
   if (not self.lastTime) or viewController:isGameOver()  then  return end
--   print("animationUpdate")
    local dt =viewController:now() -self.lastTime
    local s =self.v*dt
    local x,y =self.fish:getPosition()
    if y>display.height then
      print("game over 1")
      self.isRotate =false
      self:notifyGameOver()
      return
    end
    if y+s <100 then
      self.isRotate =false
      self.fish:setPosition(CCPoint(x,100))
      print("game over 2")
      self:notifyGameOver()
      return
    end
    if self.v<0 and not self.isRotate then
       local h =y
       local t =math.sqrt(2*h/self.g)
       self.fish:rotate(t)
       self.isRotate =true
    end
    self.fish:setPosition(CCPoint(x,y+s))
    self.v=self.v-self.g*dt
    self.lastTime =viewController:now()
    
end

function  uiLayer:notifyGameOver()
      viewController:setStateGameOver()
end




function uiLayer:collWithDaoJu()
  self.fish:collWithDaoJu()
end

function uiLayer:onTouchEvent(event,points)
   if event=="began" and not viewController.isGameOver() and not viewController.isRush() then
       print("click")
       self.lastTime =viewController.now()
       print("self.lastTime",self.lastTime)
       local x,y =self.fish:getPosition()
       local h =200
       local t =math.sqrt(2*h/self.g)
       self.v =self.g*t --初始速度
       self.isRotate =false
       self.fish:onTouch()
  
   end
end

return uiLayer
